// JORGE FERNANDO GOMEZ
// 1259371
// 189631
// LEC A1
// MARTIN MÜLLER
// D02
// YANG LIU


#include "util.h"
#include "process.h"
#include "read.h"

#include "draw1b.h"


/* Read header files for more information. */

/*
 * draw1b.c takes an input file, performs some simple parsing operations
 * through it, and outputs commands to Sketchpad.jar or stdout depending
 * on each keyword found.
 *
 * draw1b.c starts by confirming an input file was received.  If one is not
 * found, the program prints a message and exits.
 *
 * draw1b.c now checks for ./Sketchpad.jar or ./sketchpad.jar.  It it can't
 * be found, the program prints a message and exits.
 *
 * Five variables declared:
 *
 *      Figures is a struct Figure pointer.  Each index will store
 *      a single figure's definition.  That is, the figure's name
 *      its amount of draw segments, its starting (x, y) pen
 *      position, as well as (x, y) deltas.
 *
 *      Lines is a struct Line pointer.  Each index will store a single
 *      line, as well as that line's starting keyword type.
 *
 *      figureCount will keep track of the amount
 *      of figure definitions.
 *
 *      inputFile and outputFile serve as additional I/O streams.
 *
 *      inputFileLength holds the line count of the input file.
 *
 *
 * The program prints a welcome message specifying the name of the
 * executable, the current date via system("date"); and the current input
 * file name that the program will attempt to evaluate.
 *
 * draw1b.c opens the input file with fopen() and an output stream wih
 * with popen(); each of these are stored in inputFile and outputFile
 * respectively.  If either of these operations fail, the program will print
 * and error to stderr and terminate.
 *
 * Line count is computed and stored in inputFileLength (global).
 *
 * readFile() is called to read each line on the input file and store it on
 * Lines accordingly.  If readFile() encounters a problem, an error is
 * printed to stderr and program terminates.
 *
 * Program now classifies each line according to eight different keywords
 * and tags each one of them accordingly.  classifyLines() also counts
 * the number of figure definitions (repeates allowed).  All comments found
 * are echoed to stdin, except if it is preceeding the "child end" command.
 * If classifyLines() encountered bad input, an error is printed to stderr
 * and the program terminates.
 *
 * If classifyLines() counted zero figure definitions, there is nothing to
 * draw.  A message is printed to stdout, and the program exits 0.
 *
 * If figure definitions were found, getFigureSpecs() is called to store each
 * figure definition in Figures.
 *
 *
 * Now the program has the information necessary to carry out any command
 * found on in Lines. executeCommands() is called, and depending on each tag
 * at found on each line: a figure is drawn in Sketchpad.jar or its properties
 * printed to stdout with drawPrintFigure(), a figure is translated by some
 * deltas (x, y) with translateFigure() or child commands are piped to
 * Sketchpad with sendChildCommands().
 *
 *
 * The program closes its extra I/O streams with fclose() and pclose();
 * If either of these calls encountered a problem, the program prints
 * an error to stderr and terminates.  Otherwise the program
 * exits 0 successfully. */




int figureCount = 0;
int inputFileLength;

int main ( int argc, char * argv[] ) {

    /* Check environment. */
    if ( argc < 2 ) {
        printf("Please enter at least 1 input file.\n");
        exit(EXIT_SUCCESS);
    }

    char exec_c[23+1];
    FILE * sketchpad;

    if ( argc == 2 ) {

        if ( (sketchpad = fopen("Sketchpad.jar", "r")) != NULL )
            strncpy(exec_c, "java -jar Sketchpad.jar", 24);

        else if ( (sketchpad = fopen("sketchpad.jar", "r")) != NULL )
            strncpy(exec_c, "java -jar sketchpad.jar", 24);

        else {
            fprintf(stderr, "draw1b error: ./Sketchpad.jar not found. "
               " Please confirm appropriate files exist in this directory.\n");
            exit(EXIT_FAILURE);
        }
    }

    int debug = 0;
    FILE * inputFile = fopen(argv[1], "r");
    FILE * outputFile = popen(exec_c, "w");

    if ( !inputFile || !outputFile ) {
        fprintf(stderr, "draw1b error: could not open I/O stream(s). "
            " Please confirm appropriate files exist in this directory.\n");
        exit(EXIT_FAILURE);
    }


    char * buff = (char *) malloc(MAXLEN+1);
    memset(buff, '\0', (MAXLEN+1));

    for ( inputFileLength = 0;      /* Get file length (line count). */
          fgets(buff, MAXLEN+1, inputFile) != NULL;
          inputFileLength++)
        ;

    free(buff);


    Line Lines[inputFileLength];    /* Holds a line read from a file. */
    Figure Figures[MAXFIGS];      /* Holds figure definitions found. */

    int i;
    for ( i = 0; i < MAXFIGS; i++ ) {
        Figures[i].isdefined = 0;
    }

    /* Welcome message. */
    printf("%s started on ", argv[0]);
    fflush(NULL);
    system("date");
    printf("Input file: ./%s\n", argv[1]);

    /* Store file. */
    if ( (debug = readFile(Lines, inputFile)) ) {
        fprintf(stderr, "draw1b error: Could not read file.\n");
        exit(EXIT_FAILURE);
    }

    /* Classify each line stored. */
    if ( (debug = classifyLines(Lines)) ) {
        fprintf(stderr, "draw1b error: Bad input file format.\n");
        exit(EXIT_FAILURE);
    }

    /* Execute an action depending on the tag for each line. */
    executeCommands( outputFile, Lines, Figures);



    if (pclose(outputFile) == -1) {
        fprintf(stderr, "pclose() failure.\n");
        exit(EXIT_FAILURE);
    }
    fclose(inputFile);

    return(EXIT_SUCCESS);
}
